package com.vivz.baby.rest.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.vivz.baby.constant.RestServiceConstants;
import com.vivz.baby.dao.LogDAO;
import com.vivz.baby.domain.Log;
import com.vivz.baby.util.SysoutUtil;

/**
 * Log Rest Service
 *
 * @author Vivek More
 */
@RestController
@RequestMapping("/log")
public class RestLogService {

    private static final Log STUBBED_LOG;

    private static final List<Log> STUBBED_LIST;

    static {
	STUBBED_LOG = new Log();
	// STUBBED_LOG.setDiaper("DD");
	// STUBBED_LOG.setFormula(11);
	// STUBBED_LOG.setId(1010l);
	// STUBBED_LOG.setLeftBreast(33);
	// STUBBED_LOG.setRightBreast(33);
	// STUBBED_LOG.setPumped(55);
	// final Date today = new Date();
	// STUBBED_LOG.setTimestamp(today);
	// STUBBED_LOG.setUpdateTimestamp(today);

	STUBBED_LIST = Collections.singletonList(STUBBED_LOG);
    }

    @Autowired
    private LogDAO logDAO;

    @RequestMapping(value = "/add")
    public Log addLog(@RequestBody final Log log) {
	SysoutUtil.sysout(this.getClass(), "addLog()");
	final Log save = logDAO.save(log);
	return save;
    }

    @RequestMapping(value = "/delete/{userId}")
    public void deleteLog(@PathVariable final Long userId) {
	SysoutUtil.sysout("Received request to delete id [" + userId + "]");
	logDAO.delete(userId);
    }

    @RequestMapping(value = "/get/{userId}")
    @ResponseStatus(HttpStatus.OK)
    public List<Log> getLogs(@PathVariable Long userId) {
	final List<Log> allLogs = (List<Log>) logDAO.findByUserIdOrderByLogTimeDesc(userId);
	SysoutUtil.sysout(this.getClass(), "getLogs()");

	if (CollectionUtils.isEmpty(allLogs)) {
	    return STUBBED_LIST;
	}

	return new ArrayList<Log>(allLogs);
    }

    @RequestMapping(value = "/get/{userId}/today")
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<Log> getLogsForCurrentDay(@PathVariable Long userId) {
	SysoutUtil.sysout(this.getClass(), "getLogsForCurrentDay()");
	final Calendar c = Calendar.getInstance();
	final Date now = c.getTime();
	c.set(Calendar.HOUR, c.getActualMinimum(Calendar.HOUR));
	c.set(Calendar.MINUTE, c.getActualMinimum(Calendar.MINUTE));
	c.set(Calendar.SECOND, c.getActualMinimum(Calendar.SECOND));
	final Date todayMidnight = c.getTime();
	final List<Log> allLogs = logDAO.findByUserIdAndLogTimeBetween(userId, todayMidnight, now);
	if (CollectionUtils.isEmpty(allLogs)) {
	    return STUBBED_LIST;
	}

	return allLogs;
    }

    @RequestMapping(value = "/get/{userId}/thisweek")
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<Log> getLogsForCurrentWeek(@PathVariable Long userId) {
	SysoutUtil.sysout(this.getClass(), "getLogsForCurrentWeek()");
	final Calendar c = Calendar.getInstance();
	final Date now = c.getTime();
	c.set(Calendar.HOUR, c.getActualMinimum(Calendar.HOUR));
	c.set(Calendar.MINUTE, c.getActualMinimum(Calendar.MINUTE));
	c.set(Calendar.SECOND, c.getActualMinimum(Calendar.SECOND));
	c.add(Calendar.HOUR, -24 * 7);
	final Date midnightAWeekAgo = c.getTime();
	final List<Log> allLogs = logDAO.findByUserIdAndLogTimeBetween(userId, midnightAWeekAgo, now);
	if (CollectionUtils.isEmpty(allLogs)) {
	    return STUBBED_LIST;
	}

	return allLogs;
    }

    @RequestMapping(value = "/get/{userId}/thismonth")
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<Log> getLogsForCurrentMonth(@PathVariable Long userId) {
	SysoutUtil.sysout(this.getClass(), "getLogsForCurrentMonth()");
	final Calendar c = Calendar.getInstance();
	final Date now = c.getTime();
	c.set(Calendar.HOUR, c.getActualMinimum(Calendar.HOUR));
	c.set(Calendar.MINUTE, c.getActualMinimum(Calendar.MINUTE));
	c.set(Calendar.SECOND, c.getActualMinimum(Calendar.SECOND));
	c.set(Calendar.DATE, c.getActualMinimum(Calendar.DATE));
	final Date monthBeginning = c.getTime();
	final List<Log> allLogs = logDAO.findByUserIdAndLogTimeBetween(userId, monthBeginning, now);
	if (CollectionUtils.isEmpty(allLogs)) {
	    return STUBBED_LIST;
	}

	return allLogs;
    }

    @RequestMapping(value = "/get/{userId}/dates")
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<Log> getLogsBetweenDates(@PathVariable Long userId, @RequestBody final Map<String, Date> map) {
	SysoutUtil.sysout(this.getClass(), "getLogsBetween()", userId, map);
	final Date d1 = map.get(RestServiceConstants.DATE1);
	final Date d2 = map.get(RestServiceConstants.DATE2);
	final List<Log> allLogs = logDAO.findByUserIdAndLogTimeBetween(userId, d1, d2);
	if (CollectionUtils.isEmpty(allLogs)) {
	    return STUBBED_LIST;
	}

	return allLogs;
    }
}
